﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace SLUISystem
{
    public class EditorWindowDemo : EditorWindow
    {

        [MenuItem("CustomTools/Demo/DemoWindow")]
        static void OpenWindow()
        {
            EditorWindowDemo demoWindow = GetWindow(typeof(EditorWindowDemo)) as EditorWindowDemo;
            demoWindow.titleContent = new GUIContent("编辑器Demo");
            demoWindow.wantsMouseMove = true;
            demoWindow.minSize = new Vector2(600f, 500f);
            DontDestroyOnLoad(demoWindow);
        }

        private Rect ToolBarRect;
        private Rect PropertyToolBarRect;
        private Rect PropertyBoxRect;
        private Rect GraphRect;
        private Vector2 GraphScrollPosition = new Vector2(-1f, -1f);
        private Vector2 GraphOffset;
        private Vector2 GraphScrollSize = new Vector2(20000f, 20000f);

        private Vector2 CurrentMousePosition;
        private float GraphZoom=1;

        EditorMenuDemo mRightClickBlockMenu;

        private EditorDragableItem item = new EditorDragableItem();

        private void Awake()
        {
            item.InitTexture(EditorWindowUtils.LoadTexture("TaskBorder.png"));
        }

        void InitSize()
        {
            ToolBarRect= new Rect(EditorWindowConfig.PropertyBoxWidth, 0f, (Screen.width - EditorWindowConfig.PropertyBoxWidth), EditorWindowConfig.ToolBarHeight);
            PropertyToolBarRect = new Rect(0f, 0f, EditorWindowConfig.PropertyBoxWidth, EditorWindowConfig.ToolBarHeight);
            PropertyBoxRect = new Rect(0f, PropertyToolBarRect.height, EditorWindowConfig.PropertyBoxWidth, Screen.height - PropertyToolBarRect.height - EditorWindowConfig.EditorWindowTabHeight);

            GraphRect = new Rect(EditorWindowConfig.PropertyBoxWidth, 
                                    EditorWindowConfig.ToolBarHeight, 
                                    (Screen.width - EditorWindowConfig.PropertyBoxWidth - EditorWindowConfig.ScrollBarSize), 
                                    (Screen.height - EditorWindowConfig.ToolBarHeight - EditorWindowConfig.EditorWindowTabHeight - EditorWindowConfig.ScrollBarSize));
        }

        private void OnGUI()
        {
            InitSize();
            CurrentMousePosition = Event.current.mousePosition;
            HandleInputEvents();
            DrawToolBar();
            DrawProperties();
            DrawGraphArea();
        }

        private void Update()
        {
            if (Selection.activeObject)
            {
                //DemoAgent agent = ((GameObject)Selection.activeObject).GetComponent<DemoAgent>();
                //Debug.Log(agent != null);
            }
        }

        private void DrawToolBar()
        {
            GUILayout.BeginArea(ToolBarRect, EditorStyles.toolbar);
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Test1",EditorStyles.toolbarButton, GUILayout.Width(80f)))
            {
                Debug.Log("test1");
            }
            if (GUILayout.Button("Test2", EditorStyles.toolbarButton, GUILayout.Width(80f)))
            {
                Debug.Log("test2");
            }

            GUILayout.FlexibleSpace();

            if (GUILayout.Button("Test3", EditorStyles.toolbarButton, GUILayout.Width(80f)))
            {
                Debug.Log("test3");
            }
            GUILayout.EndHorizontal();
            GUILayout.EndArea();
        }

        private void DrawProperties()
        {
            GUILayout.BeginArea(PropertyToolBarRect, EditorStyles.toolbar);

            GUILayout.EndArea();

            GUILayout.BeginArea(PropertyBoxRect);
            GUILayout.Label("Test");
            GUILayout.EndArea();
        }

        private void DrawGraphArea()
        {

            Vector2 vector = GUI.BeginScrollView(new Rect(GraphRect.x, GraphRect.y, GraphRect.width + EditorWindowConfig.ScrollBarSize, GraphRect.height + EditorWindowConfig.ScrollBarSize), GraphScrollPosition, new Rect(0f, 0f, GraphScrollSize.x, GraphScrollSize.y), true, true);
            if (vector != GraphScrollPosition && Event.current.type != EventType.DragUpdated && Event.current.type != EventType.Ignore)
            {
                //mGraphOffset -= (vector - GraphScrollPosition) / mGraphZoom;
                GraphScrollPosition = vector;
                //mGraphDesigner.graphDirty();
            }
            GUI.EndScrollView();

            GUI.Box(GraphRect, "", EditorWindowConfig.GraphBackgroundGUIStyle);

            EditorZoomArea.Begin(GraphRect, GraphZoom);
            //GUI.Label(rect, "", m_Selected ? BTreeEditorUtility.TaskSelectedGUIStyle : BTreeEditorUtility.TaskGUIStyle);
            Vector2 originPos = new Vector2(500,500);
            EditorGraphDrawer.DrawTexture(originPos - GraphScrollPosition);
            item.DrawItem(GraphScrollPosition);
            EditorZoomArea.End();
        }


        private void HandleInputEvents()
        {
            if (EditorApplication.isCompiling) return;

            Event e = Event.current;
            switch (e.type)
            {
                case EventType.MouseDown:
                    if (e.button == 0)
                    {
                        Vector2 point;
                        Debug.Log("item.rectInGraph" + item.rectInGraph+ IsMousePositionInRect(item.rectInGraph, out point)+ point);


                        if (IsMousePositionInRect(GraphRect, out point))
                        {
                            Debug.Log("鼠标左键点击  " + IsMousePositionInRect(GraphRect, out point) + "  " + point);
                            if (item.rectInGraph.Contains(point))
                            {
                                item.IsClicking = true;
                                Debug.Log("鼠标左键点击中item  " + item.rectInGraph.Contains(point) + "  " + point);
                                e.Use();
                            }
                        }

                    }
                    else if (e.button == 1)
                    {
                        Debug.Log("鼠标右键键点击");
                        e.Use();
                    }
                    else if (e.button == 2)
                    {
                        Debug.Log("鼠标中键键点击");
                        e.Use();
                    }
                    break;
                case EventType.MouseUp:
                    if (e.button == 0)
                    {
                        item.IsClicking = false;
                        Debug.Log("鼠标左键抬起");
                    }
                    else if (e.button == 1)
                    {
                        Debug.Log("鼠标右键键抬起");
                        if (mRightClickBlockMenu == null)
                        {
                            mRightClickBlockMenu = new EditorMenuDemo(this);
                        }
                        mRightClickBlockMenu.ShowAsContext();
                    }
                    else if (e.button == 2)
                    {
                        Debug.Log("鼠标中键键抬起");
                    }
                    break;
                case EventType.MouseMove:
                    if (e.button == 0)
                    {
                        Debug.Log("鼠标左键移动");
                    }
                    else if (e.button == 1)
                    {
                        Debug.Log("鼠标右键键移动");
                    }
                    else if (e.button == 2)
                    {
                        Debug.Log("鼠标中键键移动");
                    }
                    break;
                case EventType.MouseDrag:
                    if (e.button == 0)
                    {
                        if (item.IsClicking)
                        {
                            item.SetPosition(item.position + e.delta);
                            Repaint();
                        }
                        Debug.Log("鼠标左键拖动");
                    }
                    else if (e.button == 1)
                    {
                        Debug.Log("鼠标右键键拖动");
                    }
                    else if (e.button == 2)
                    {
                        Debug.Log("鼠标中键键拖动");
                    }
                    break;
                case EventType.KeyDown:
                    break;
                case EventType.KeyUp:
                    break;
                case EventType.ScrollWheel:
                    Debug.Log("鼠标中键滚动");
                    GraphZoom += e.delta.y * 0.01f;
                    GraphZoom = Mathf.Clamp(GraphZoom, 1, 5);
                    e.Use();
                    break;
                case EventType.Repaint:
                    break;
                case EventType.Layout:
                    break;
                case EventType.DragUpdated:
                    break;
                case EventType.DragPerform:
                    break;
                case EventType.DragExited:
                    break;
                case EventType.Ignore:
                    break;
                case EventType.Used:
                    break;
                case EventType.ValidateCommand:
                    break;
                case EventType.ExecuteCommand:
                    break;
                case EventType.ContextClick:
                    break;
                default:
                    break;
            }
        }

        private bool IsMousePositionInRect(Rect rect,out Vector2 mousePosition)
        {
            mousePosition = CurrentMousePosition;
            if (!rect.Contains(mousePosition))
            {
                return false;
            }
            mousePosition -= new Vector2(rect.xMin, rect.yMin);
            //mousePosition /= mGraphZoom;
            return true;
        }
    }
}

